package com.itheima.service.mongo.impl;

import com.itheima.domain.db.PageBeanVo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.CommentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer saveComment(Comment comment) {

        mongoTemplate.save(comment);

        switch (comment.getCommentType()) {

            case 1: {

                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

                movement.setLikeCount(movement.getLikeCount() + 1);

                mongoTemplate.save(movement);

                return movement.getLikeCount();

            }

            case 2: {
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

                movement.setCommentCount(movement.getCommentCount() + 1);

                mongoTemplate.save(movement);

                return movement.getCommentCount();
            }
            case 3: {
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

                movement.setLoveCount(movement.getCommentCount() + 1);

                mongoTemplate.save(movement);

                return movement.getLoveCount();
            }
            case 4: {
            }
            case 5: {
            }
            case 6: {
            }
        }

        return 0;

           /* // 1.保存comment
            mongoTemplate.save(comment);
            // 2.根据commentType类型判断
            switch (comment.getCommentType()) {
                case 1: { // 点赞
                    // 先查询动态
                    Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                    // 点赞数量+1 并更新到mongo中
                    movement.setLikeCount(movement.getLikeCount() + 1);
                    mongoTemplate.save(movement);
                    // 返回数量
                    return movement.getLikeCount();
                }
                case 2: { // 评论
                    // 先查动态
                    Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                    // 评论数量+1 并更新到mongo中
                    movement.setCommentCount(movement.getCommentCount() + 1);
                    mongoTemplate.save(movement);
                    // 返回数量
                    return movement.getCommentCount();
                }
                case 3: { // 喜欢
                    // 先查动态
                    Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                    // 喜欢数量+1 并更新到mongo中
                    movement.setLoveCount(movement.getLoveCount() + 1);
                    mongoTemplate.save(movement);
                    // 返回数量
                    return movement.getLoveCount();
                }
                case 4: {
                }
                case 5: {
                }
                case 6: {
                }
            }
            return 0;*/
    }

    @Override
    public Integer removeComment(Long userId, ObjectId publishId, Integer commentType) {

        Query query = new Query(Criteria.where("userId").is(userId).
                and("publishId").is(publishId).
                and("commentType").is(commentType));


        mongoTemplate.remove(query, Movement.class);

        switch (commentType) {

            case 1: {

                Movement movement = mongoTemplate.findById(publishId, Movement.class);

                movement.setLikeCount(movement.getLikeCount() - 1);

                mongoTemplate.save(movement);

                return movement.getLikeCount();

            }

            case 2: {

                Movement movement = mongoTemplate.findById(publishId, Movement.class);

                movement.setCommentCount(movement.getCommentCount() - 1);

                mongoTemplate.save(movement);

                return movement.getCommentCount();
            }

            case 3: {

                Movement movement = mongoTemplate.findById(publishId, Movement.class);

                movement.setLoveCount(movement.getLoveCount() - 1);

                mongoTemplate.save(movement);

                return movement.getLoveCount();
            }

            case 4: {
            }

            case 5: {
            }

            case 6: {
            }
        }
        return 0;
    }

    @Override
    public PageBeanVo findCommentByPage(ObjectId publishId, Integer commentType, Integer pageNum, Integer pageSize) {

        Query query = new Query(Criteria.where("publishId").is(publishId).and("commentType").is(commentType)).with(Sort.by(Sort.Order.desc("created"))).skip((pageNum - 1) * pageSize).limit(pageSize);

        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        long count = mongoTemplate.count(new Query(
                Criteria.where("publishId").is(publishId)
                        .and("commentType").is(commentType)), Comment.class);

        return new PageBeanVo(pageNum, pageSize, count, comments);
    }
}
